
package hasthi.manager.monitering;

import hasthi.common.HasthiException;
import hasthi.common.XmlBeanUtils;
import hasthi.common.HasthiException.FaultCode;
import hasthi.manager.ManagerContext;

import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;

import javax.xml.namespace.QName;

import org.apache.commons.jelly.JellyContext;
import org.apache.commons.jelly.JellyException;
import org.apache.commons.jelly.XMLOutput;
import org.apache.xmlbeans.XmlObject;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

public class JellyTriggerExecuter implements EventListener{
    private ManagerContext managerContext;
    
    public JellyTriggerExecuter(ManagerContext managerContext) {
        this.managerContext = managerContext;
    }

    public String getSearchPath() {
        return null;
    }

    public void performTriggerAction(XmlObject event, XmlObject[] matchingResults) throws HasthiException {
        String faultPath = "declare namespace env='http://www.w3.org/2003/05/soap-envelope';" +
        "$this//env:Fault/env:Code/env:Value";

        try {
            XmlObject[] faultData = event.selectPath(faultPath);
            if(faultData != null && faultData.length > 0){
                QName faultCode = XmlBeanUtils.getQNameValue(faultData[0]);
                String faultURI = faultCode.getNamespaceURI()+"/"+faultCode.getLocalPart();
//                String actionURI = managerContext.getEvent2ActionRepository().getAction4Event(faultURI);
//                runJellyScript(event, actionURI);
                System.out.println(faultURI);
            }
        } catch (Exception e) {
            throw new HasthiException(e, FaultCode.UnsupportedMessage);
        }
    }
    
    
    public String getTextValue(Node node){
        NodeList nodes = node.getChildNodes();
        if(nodes != null){
            StringBuffer str =  new StringBuffer();
            for(int i = 0;i<nodes.getLength();i++){
                
                Node child = nodes.item(i);
                if(child.getNodeType() == Node.TEXT_NODE){
                    str.append(child.getNodeValue());
                }
            }
            return str.toString();
        }
        return null;
    }
    
    
    
    
    
    
    
    private void runJellyScript(XmlObject event,String actionURI) throws HasthiException{
        try {
            XmlObject action = managerContext.getActionRepository().getAction(actionURI);
            JellyContext context = new JellyContext();
            context.setVariable("event",event.xmlText());
            StringWriter output = new StringWriter();
            XMLOutput xmlOutput = XMLOutput.createXMLOutput(output);
            
            InputSource inSource = new InputSource(new StringReader(action.xmlText()));
            context.runScript(inSource, xmlOutput);
            xmlOutput.flush();
            System.out.println("Action "+actionURI + "execuated with result "+output.getBuffer().toString());
        } catch (JellyException e) {
            throw new HasthiException(e, FaultCode.LocalError);
        } catch (IOException e) {
            throw new HasthiException(e,FaultCode.LocalError);
        }
    }
    
    public static class Message{
        private String value = "Hello223";
        public String getValue() {
            return value;
        }
    }
    
    
    public static void main(String[] args) throws Exception{
        String script = "<j:jelly xmlns:j=\"jelly:core\">\n${msg.value}\n</j:jelly>";
        JellyContext context = new JellyContext();
        context.setVariable("msg", new Message());
        StringWriter output = new StringWriter();
        XMLOutput xmlOutput = XMLOutput.createXMLOutput(output);
        
        
        
        InputSource inSource = new InputSource(new StringReader(script));
        context.runScript(inSource, xmlOutput);
        xmlOutput.flush();
        
        System.out.println(output.getBuffer().toString());
    }

}

